package com.nanhu.test.widget.compoundview;

import com.nanhu.test.R;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TengxunMenuBar extends HorizontalScrollView{
	//每一个menu的宽度
	private int blockWidth = 80;
	//menu的个数
	private int blockNumber;
	//当前是第几个menu
	private int currentIndex;
	//当前完全显示的第一个menu的位置
	private int startIndex;
	//当前完全显示的最后一个menu的位置
	private int endIndex;
	//当前x方向偏移量
	private int currentOffsetX;
	//控件layout size
	private int widgetLayoutWidth;
	//控件实际measured宽度: blockWidth * blockNumber
	private int widgetMeasuredWidth;
	//标识当前位置的图标
	private ImageView cursor;
	//图标相对于block的偏移量
	private int cursorOffset;
	
	private int lastScrollOffX;
	
	private Handler handler;
	
	float fromX = 0.0f;
	
	private OnMeneChangeListener onMeneChangeListener;
	
	public TengxunMenuBar(Context context, String[] menus, int screenW) {
		super(context);
		this.widgetLayoutWidth = screenW;
		handler = new Handler();
		initView(context, menus);
		setHorizontalScrollBarEnabled(false);
	}
	
	private void initView(Context context, String[] menus){
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.nanhu_menu_layout, this, true);
		
		LinearLayout linearLayoutMenu = (LinearLayout)findViewById(R.id.linearLayoutMenu);
		LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(blockWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
		for(int i = 0; i < menus.length; ++i){
			TextView tvMenu = new TextView(context);
			tvMenu.setPadding(0, 14, 0, 10);
			tvMenu.setText(menus[i]);
			tvMenu.setTag(i);
			tvMenu.setTextColor(Color.WHITE);
			tvMenu.setGravity(Gravity.CENTER_HORIZONTAL);
			tvMenu.setOnClickListener(clickListener);
			linearLayoutMenu.addView(tvMenu,layoutParam);
		}
		
		cursor = (ImageView) findViewById(R.id.cursor);
		int bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a).getWidth();// 获取图片宽度
		cursorOffset = (blockWidth - bmpW) / 2;// 计算偏移量
		Matrix matrix = new Matrix();
		matrix.postTranslate(cursorOffset, 0);
		cursor.setImageMatrix(matrix);// 设置动画初始位置
		
		blockNumber = menus.length;
		currentOffsetX = 0;
		currentIndex = 0;
		widgetMeasuredWidth = blockWidth * blockNumber;
		startIndex = 0;
		endIndex = widgetLayoutWidth / blockWidth;
	}
	

	
	private int computeToOffsetX(int toIndex){
		int offset = 0;
		//判断滚动条是否被手动滚动过
		boolean bNeedScroll = false;
		int scrollW = this.getScrollX();
		int toBlockLeft = blockWidth * toIndex;
		int toBlockRight = toBlockLeft + blockWidth; 
		if(toBlockLeft < scrollW || toBlockRight > scrollW + widgetLayoutWidth)
			bNeedScroll = true;
		
		
		//判断是否在startIndex 和endIndex内
		if(bNeedScroll){
			boolean toLeft = (toIndex >= currentIndex);
			if(toLeft){
				offset = (toIndex + 1) * blockWidth - widgetLayoutWidth;
			}else{
				offset = toIndex * blockWidth;
			}
			if(offset < 0)
				offset = 0;
		}else{
			offset = -1;
		}

		return offset;
	}
	
	private void scrollMenuBar(int toIndex){
		final int scrollOffX = computeToOffsetX(toIndex);
		if(scrollOffX >= 0){
			handler.postDelayed(new Runnable(){

				@Override
				public void run() {
					TengxunMenuBar.this.scrollTo(scrollOffX, 0);
				}
    			
    		}, 80);
		}
	}
	
	public void moveCursorTo(int fromIndex, float movePercent){
		float toX = fromIndex * blockWidth + blockWidth * movePercent;
		Animation animation = new TranslateAnimation(fromX, toX, 0, 0);
    	animation.setFillAfter(true);// True:图片停在动画结束位置
		animation.setDuration(0);
		cursor.startAnimation(animation);
		fromX = toX;
	}
	
	public void goTo(int toIndex, boolean isMoveCursor){
		//fromX = toIndex * blockWidth;
		scrollMenuBar(toIndex);
		if(isMoveCursor)
			moveCursorTo(toIndex,0.0f);
		currentIndex = toIndex;
	}
	
	private OnClickListener clickListener = new OnClickListener(){

		@Override
		public void onClick(View v) {
			TextView tv = (TextView)v;
			int index = (Integer)tv.getTag();
			onMeneChangeListener.onMenuChange(index);
			goTo(index, false);
		}
		
	};
	
	public void setOnMenuChangeListener(OnMeneChangeListener onMeneChangeListener){
		this.onMeneChangeListener = onMeneChangeListener;
	}
	
	public interface OnMeneChangeListener{
		public void onMenuChange(int toIndex);
	}
}
